	package com.niuwa.p2p.action.resource;
	
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.entity.resource.Resource;
import com.niuwa.p2p.opr.utils.ResourceUtil;
import com.niuwa.p2p.service.resource.ResourcesService;
import com.opensymphony.xwork2.ModelDriven;
	
	@Namespace("/resource")
	@Controller
	public class ResourceAction extends JsonAction implements ModelDriven<Object> {
	
		private static final long serialVersionUID = 2628491906933451988L;
	
		@Autowired
		ResourcesService resourcesService;
		// 资源类
		private Resource resource;
		// 资源工具类
		ResourceUtil resourceUtil;
		// 与角色相对应的资源
		List<Resource> resourceList = new ArrayList<Resource>();
		// 所有资源
		List<Resource> AllResourceList = new ArrayList<Resource>();
		// 资源权限树
		List<Resource> resourceTree = new ArrayList<Resource>();
		//角色ID
		String roleId;

		public String getRoleId() {
			return roleId;
		}


		public void setRoleId(String roleId) {
			this.roleId = roleId;
		}


		/**
		 * 
		 *查询资源
		 *@param
		 * @return
		 *
		 */
		@SuppressWarnings("unchecked")
		@Action("seachResources")
		public String seachResources() {
			ResourceUtil resourceUtil = new ResourceUtil();
			String returnValue = resourceUtil.checkSession();
			if ("login".equals(returnValue)) {
				return returnValue;
			}
			resourceList = resourceUtil.getSessionResources();
			results = new JsonResult();
			results.setData(resourceList);
			results.setSuccess(true);
			results.setMsg("");
			setResult(results);
			return returnValue;
	
		}
	
		
		/**
		 * 
		 *查询所有资源
		 * @return
		 *
		 */
		@SuppressWarnings("unchecked")
		@Action("seachAllResources")
		public String seachAllResources() {
			String returnValue = resourceUtil.checkSession();
			if ("login".equals(returnValue)) {
				return returnValue;
			}
			resourceList = resourceUtil.getSessionResources();
			AllResourceList = resourcesService.searchAllResources();
			results = new JsonResult();
			results.setData(resourceList);
			results.setSuccess(true);
			results.setMsg("");
			setResult(results);
			return "resource";
	
		}
	  
		/**
		 * 
		 *跳转权限页面
		 *@param
		 * @return
		 *
		 */
		@Action("getTreeResourcesPage")
		public String getTreeResourcesPage() {
			HttpServletRequest request = ServletActionContext.getRequest();
			roleId=request.getParameter("roleId");
			return "resource";
		}
	
		/**
		 * 
		 *得到权限树
		 * @return
		 * @throws Exception
		 *
		 */
		@SuppressWarnings("unchecked")
		@Action("getTreeResources")
		public String getTreeResources() throws Exception {
			String resourceNo = "no_0";
			HttpServletRequest request = ServletActionContext.getRequest();
		    roleId=request.getParameter("roleId");
			HttpServletResponse hsr = ServletActionContext.getResponse();
			hsr.setContentType("application/javascript;charset=UTF-8");
			@SuppressWarnings("unchecked")
			List<Resource> resourceList = (List<Resource>)resourcesService.getResourceByRoleId(Integer.parseInt(roleId)) ;
			List<Resource> AllResourceList = resourcesService.searchAllResources();
			resourceTree = treeMenuList(AllResourceList, resourceNo, resourceList);
			json = JSONObject.toJSONString(resourceTree);
			try {
				hsr.getWriter().print(json);
				hsr.getWriter().flush();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				hsr.getWriter().close();
			}
			return "none";
		}
	
		/**
		 * 
		 * 菜单树形结构
		 * @param resourceList
		 * @param parentId
		 * @return
		 *
		 */
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public List treeMenuList(List<Resource> allResourceList, String parentId,
				List<Resource> resourceList) {
			List childMenu = new ArrayList();
			for (Resource resource : allResourceList) {
				String resourceNo = resource.getNumber();
				String pid = resource.getPid();
				if (parentId.equalsIgnoreCase(pid)) {
					Map<String, Object> resourceMap = new HashMap<String, Object>();
					List c_node = treeMenuList(allResourceList, resourceNo,resourceList);
					for (Resource userResource : resourceList) {
						if (resource.getNumber().equalsIgnoreCase(
								userResource.getNumber())) {
							resourceMap.put("checked", "true");
							break;
						}
					}
					resourceMap.put("id", resource.getNumber());
					resourceMap.put("text", resource.getDescription());
					resourceMap.put("children", c_node);
					childMenu.add(resourceMap);
				}
			}
			return childMenu;
		}
		
		/**
		 * 
		 *为权限授权
		 *@param
		 * @return
		 *
		 */
		@Action("authorize")
		public String updateAuthorize () {
		HttpServletRequest request = ServletActionContext.getRequest();
		 String roleId= request.getParameter("roleId");  
		 String[] resourceNoArr=request.getParameterValues("resourceNo[]");
		 String[] unCheckResourceNoArr=request.getParameterValues("unCheckResourceNo[]");
		 resourcesService.updateAuthorize(roleId,resourceNoArr,unCheckResourceNoArr);
		 return NONE;
		}
	
		public JsonResult getResults() {
			return results;
		}
	
		public void setResults(JsonResult results) {
			this.results = results;
		}
	
		public List<Resource> getResourceList() {
			return resourceList;
		}
	
		public void setResourceList(List<Resource> resourceList) {
			this.resourceList = resourceList;
		}
	
		@Override
		public Object getModel() {
			if (null == resource) {
				return resource = new Resource();
			}
			return resource;
		}
	
		public Resource getResource() {
			return resource;
		}
	
		public void setResource(Resource resource) {
			this.resource = resource;
		}
	
		public ResourcesService getResourcesService() {
			return resourcesService;
		}
	
		public void setResourcesService(ResourcesService resourcesService) {
			this.resourcesService = resourcesService;
		}
	
		Object json = new Object();
	
		JsonResult results = new JsonResult();
	
		public Object getJson() {
			return json;
		}
	
		public void setJson(Object json) {
			this.json = json;
		}
	
		public List<Resource> getAllResourceList() {
			return AllResourceList;
		}
	
		public void setAllResourceList(List<Resource> allResourceList) {
			AllResourceList = allResourceList;
		}
	
		public List<Resource> getResourceTree() {
			return resourceTree;
		}
	
		public void setResourceTree(List<Resource> resourceTree) {
			this.resourceTree = resourceTree;
		}
		
	

	}
